package com.ruoyi.system.controller;

import com.ruoyi.common.config.file.FileUtils;
import com.ruoyi.common.config.imageZipConfig.HbhdConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ImgBrowseService;
import com.ruoyi.system.service.imgUploadService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Calendar;

/**
 * 图片上传控制层
 */
@Controller
@RequestMapping("/system/imgUpload")
public class imgUploadController extends BaseController {

    private String prefix = "other/imgUpload";

    @Autowired
    private com.ruoyi.system.service.imgUploadService imgUploadService;

    @Autowired
    private ImgBrowseService imgBrowseService;

    @RequiresPermissions("system:user:imgUpload")
    @GetMapping()
    public String imgUpload(ModelMap mmap){
        mmap.put("businessId","123");
        return prefix + "/imgUpload";
    }

    @PostMapping("/uploadImageSave")
    @ResponseBody
    public AjaxResult uploadImage(@RequestParam(value = "txt_file") MultipartFile[] files, HttpServletRequest request) {
        // 取身份信息
        SysUser user = getSysUser();
       // businessService.imgBrowse(files, id);
        imgUploadService.uploadImage(files,request,user);
        return success();
    }

    /**
     * 导出图片文件
     */
    //@RequiresPermissions("archive:inquiryWork:exportImg")
    @PostMapping("/exportImg")
    @ResponseBody
    public AjaxResult exportdisk(String id, HttpServletResponse response) {
//        ArcFileinfo info = iArcFileinfoService.selectArcFileinfoById(id);

//        String base_dir = fileComfrim;
        String base_dir = "D:/fileComfrim";

        //初始化时间日期
        Calendar calendar = Calendar.getInstance();
//        String dateStr = DateUtils.parseDateToStr("yyyy-MM-dd", info.getAPPLYDATE());
        String dateStr = DateUtils.parseDateToStr("yyyy-MM-dd", calendar.getTime());
//        String dir_name = info.getFILESEQNUM() + "-" + info.getIDCARD();
        String dir_name = "FILESEQNUM()" + "-" + "IDCARD()";

        String path = base_dir + File.separatorChar + dateStr + File.separatorChar + dir_name;
        String to_path = HbhdConfig.getDownloadPath() + dir_name + ".zip";
        FileUtils.zipFiles(path, "", to_path);
        return AjaxResult.success(dir_name + ".zip");
    }

    @GetMapping("/donlog")
    @ResponseBody
    private void download(String path,HttpServletResponse response){
        try {
            String path2 = "D:\\uploadImagePath\\2024-11-17\\a2ce5c40b1a640609537a46346cdcfdd_捕获.PNG";
            File f = new File(path2);

            BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
            byte[] buf = new byte[1024];
            int len = 0;

            response.reset(); // 非常重要

            response.setContentType("application/x-msdownload");
            response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
            OutputStream out = response.getOutputStream();
            while ((len = br.read(buf)) > 0)
                out.write(buf, 0, len);
            br.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




}
